package it.asvp.asvp_portal.service.services.impl;

import it.asvp.asvp_portal.data.dao.MemberAccountDao;
import it.asvp.asvp_portal.data.entity.member.MemberAccount;
import it.asvp.asvp_portal.service.util.UserDetailsImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserDetailsServiceImpl implements UserDetailsService{

	@Autowired
	MemberAccountDao memberAccountDao;
	
	@Transactional
	@Rollback
	public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException, DataAccessException {
		
		MemberAccount memberAccount=memberAccountDao.findByLogin(login);
		
		if(memberAccount==null ){
			
			throw new UsernameNotFoundException("Account errato");
		}
		
		if(memberAccount.isEnabled()==false){
			
		}
		
		UserDetails userDetails=new UserDetailsImpl(memberAccount);
		
		return userDetails;
	}

}
